﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using MadeInPortugal.Class;
using MadeInPortugal;

namespace MadeInPortugalWebApplication.Handler
//{
//    public class ImageHandler
//    {
//    }
//}
//using System;
//using System.Data;
//using System.Configuration;
//using System.Web;
//using System.Web.Security;
//using System.Web.UI;
//using System.Web.UI.HtmlControls;
//using System.Web.UI.WebControls;
//using System.Web.UI.WebControls.WebParts;
//using System.Drawing;

//namespace MyDesktopSite.Handlers
{
    public class ImageHandler : IHttpHandler
    {
        //private MyDesktopBusiness.MyDesktopService _businessService = new MyDesktopBusiness.MyDesktopService();

        #region IHttpHandler Members

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            string[] urlPaths = context.Request.Url.ToString().Split('/');
            if (urlPaths != null && urlPaths.Length > 0)
            {
                string filecode = urlPaths[urlPaths.Length - 1].Replace(".image", "");
                string filepath = urlPaths[urlPaths.Length - 2];

                try
                {
                    string imageType = string.Empty;
                    switch (filepath)
                    {
                        //case "background":
                        //    imageType = "background";
                        //    break;

                        case "smallIcons":
                            imageType = "smal";
                            break;
                        case "bigIcons":
                            imageType = "big";
                            break;
                        
                        default:
                            return;
                    }
                    byte[] img;
                    if (imageType.Equals("smal"))
                    {
                        LogoImage logo = new LogoImage();
                        logo = logo.getImage(int.Parse(filecode));
                        img = logo.Logo;

                    }
                    else
                    {
                        //if (imageType.Equals("background"))
                        //{
                        //    byte[] resp = _businessService.GetUserImage(int.Parse(filecode));
                        //    if (resp != null)
                        //        context.Response.BinaryWrite(resp);
                        //    return;
                        //}
                        //byte[] img = _businessService.GetItemImage(int.Parse(filecode), imageType);
                        ProductImage prodImage = new ProductImage();
                        prodImage = prodImage.getImage(int.Parse(filecode));
                        img = prodImage.Image;
                    }
                    if (img != null)
                    {
                       // ImageConverter converter = new ImageConverter();
                        context.Response.BinaryWrite(img);
                    }
                    else
                    {
                        System.Drawing.Image imgDefault = System.Drawing.Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "images\\" + filepath + "\\noImage.jpg");
                        if (imgDefault != null)
                        {
                            ImageConverter converter = new ImageConverter();
                            context.Response.BinaryWrite((byte[])converter.ConvertTo(imgDefault, typeof(byte[])));
                        }
                    }
                }
                catch (Exception ex) 
                {
                    bool b = false;
                    if (! (ex is IndexOutOfRangeException))
                    {
                        b = true;
                    }
                    System.Drawing.Image imgDefault = System.Drawing.Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "style\\img\\" + filepath + "\\noImage.jpg");
                    if (imgDefault != null)
                    {
                        ImageConverter converter = new ImageConverter();
                        context.Response.BinaryWrite((byte[])converter.ConvertTo(imgDefault, typeof(byte[])));
                    }
                }
                
            }
        }

        #endregion
    }
}
